/*
 * Cmd.cpp
 *
 *  Created on: 2012-10-8
 *      Author: yangentao@gmail.com
 */
#include "Cmd.h"

#include "../src/winui.h"
using namespace std;

CmdItem::CmdItem() {
	fVirt = 0;
	key = 0;
	cmd = 0;
	when = 0;
	category = 0;
}

CmdItem::~CmdItem() {
}

CmdItem& CmdItem::Cmd(WORD cmd_) {
	cmd = cmd_;
	return *this;
}

CmdItem& CmdItem::Ctrl(WORD key_) {
	key = key_;
	fVirt = FCONTROL;
	return *this;
}
CmdItem& CmdItem::Alt(WORD key_) {
	key = key_;
	fVirt = FALT;
	return *this;
}
CmdItem& CmdItem::Shift(WORD key_) {
	key = key_;
	fVirt = FSHIFT;
	return *this;
}

CmdItem& CmdItem::CtrlShift(WORD key_) {
	key = key_;
	fVirt =FCONTROL | FSHIFT ;
	return *this;
}
CmdItem& CmdItem::CtrlAlt(WORD key_) {
	key = key_;
	fVirt = FCONTROL | FALT;
	return *this;
}
CmdItem& CmdItem::AltShift(WORD key_) {
	key = key_;
	fVirt = FALT | FSHIFT;
	return *this;
}
CmdItem& CmdItem::CtrlAltShift(WORD key_) {
	key = key_;
	fVirt = FCONTROL | FALT | FSHIFT;
	return *this;
}

CmdItem& CmdItem::Text(String text_) {
	text = text_;
	return *this;
}
CmdItem& CmdItem::Desc(String desc_) {
	desc = desc_;
	return *this;
}
CmdItem& CmdItem::clear() {
	key = 0;
	cmd = 0;
	fVirt = 0;
	text.clear();
	desc.clear();
	return *this;
}
String CmdItem::getDesc() {
	return desc.empty() ? text : desc;
}

String CmdItem::getMenuItemText() {
	String s = text;
	if (key) {
		s.append("\t");
		if (fVirt) {
			if (fVirt & FCONTROL) {
				s.append("Ctrl+");
			}
			if (fVirt & FALT) {
				s.append("Alt+");
			}
			if (fVirt & FSHIFT) {
				s.append("Shift+");
			}
		}
		s.append(char(upperChar(key)));
	}

	return "";
}

Vector<CmdItem> cmds(64);

SparseArray<CmdItem> cmdMap(128);

void loadDefaults() {
	if (cmds.getTag() > 0) {
		return;
	}

	CmdItem item;
	item.clear().Cmd(CMD_FILE_NEW).Ctrl('n').Text("New");
	cmdMap.put(CMD_FILE_NEW, item);
	cmds.append(item);

	item.clear().Cmd(CMD_FILE_OPEN).Ctrl('o').Text("Open");
	cmdMap.put(CMD_FILE_OPEN, item);
	cmds.append(item);

	item.clear().Cmd(CMD_FILE_SAVE).Ctrl('s').Text("Save");
	cmdMap.put(CMD_FILE_SAVE, item);
	cmds.append(item);

	item.clear().Cmd(CMD_FILE_SAVE_ALL).CtrlShift('s').Text("Save All");
	cmdMap.put(CMD_FILE_SAVE_ALL, item);
	cmds.append(item);

	item.clear().Cmd(CMD_FILE_EXIT).Alt('x').Text("Exit");
	cmdMap.put(CMD_FILE_EXIT, item);
	cmds.append(item);

	cmds.setTag(1);
}
HMENU  createMainMenu() {
	MenuBuilder mFile;
	mFile.CreateAsSub();
	mFile.AppendItem(CMD_FILE_NEW, L"New\tCtrl+N");
	mFile.AppendItem(CMD_FILE_OPEN, L"Open\tCtrl+O");
	mFile.AppendItem(CMD_FILE_SAVE, L"Save\tCtrl+S");
	mFile.AppendItem(CMD_FILE_SAVE_ALL, L"Save All\tCtrl+Shift+S");
	mFile.AppendSeprator();
	mFile.AppendItem(CMD_FILE_EXIT, L"Exit\tAlt+X");

	MenuBuilder menuBar;
	menuBar.Create();
	menuBar.AppendSubMenu(L"File", mFile);
	return menuBar;
}
HACCEL loadAccel() {
	loadDefaults();
	ACCEL* accels = new ACCEL[cmds.size()];
	int n = 0;
	for (int i = 0; i < cmds.size(); ++i) {
		if (cmds[i].key) {
			accels[n] = ((ACCEL)cmds[i]);
			++n;
		}
	}
	HACCEL hacc =  CreateAcceleratorTable(accels, n);
	delete[] accels;
	return hacc;
}

